package com.yeren.weixin.gzh.weixin.service.impl;

import java.util.List;

import com.yeren.weixin.gzh.weixin.dto.OriginalUserInfoDTO;
import com.yeren.weixin.gzh.weixin.dto.WeixinGroupDTO;
import com.yeren.weixin.gzh.weixin.dto.WeixinQRCodeDTO;
import com.yeren.weixin.gzh.weixin.dto.WeixinUserListDTO;
import com.yeren.weixin.gzh.weixin.message.makeCustomMessage.MakeCustomMessage;
import com.yeren.weixin.gzh.weixin.operation.GetUserInfoExecute;
import com.yeren.weixin.gzh.weixin.operation.GroupExecute;
import com.yeren.weixin.gzh.weixin.operation.QRCodeExecute;
import com.yeren.weixin.gzh.weixin.operation.TokenExecute;
import com.yeren.weixin.gzh.weixin.service.WeixinService;


/**
 * liubiao
 */
public class WeixinServiceImpl implements WeixinService {
	//私人服务器给微信用户发文字消息
	public void sendCustomTextMessage(String appid, String appsecret,String openId, String content) {
		// 获取接口访问凭据
		String accessToken = TokenExecute.getToken(appid, appsecret).getAccessToken();
		// 组装文本客服消息
		String jsonTextMsg = MakeCustomMessage.makeTextCustomMessage(openId, content);
		// 发送客服消息
		MakeCustomMessage.sendCustomMessage(accessToken, jsonTextMsg);
	}
	
	//获取单个用户信息
	public void getUserInfo(String appid, String appsecret, String openId) {
		//获取接口访问凭据
				String accessToken=TokenExecute.getToken(appid, appsecret).getAccessToken();
				OriginalUserInfoDTO originalUserInfo = GetUserInfoExecute.getUserInfo(accessToken, openId);

				System.out.println("OpenID：" + originalUserInfo.getOpenId());
				System.out.println("关注状态：  " + originalUserInfo.getSubscribe());
				System.out.println("关注时间：  " + originalUserInfo.getSubscribeTime());
				System.out.println("昵称：          " + originalUserInfo.getNickname());
				System.out.println("性别：          " + originalUserInfo.getSex());
				System.out.println("国家：          " + originalUserInfo.getCountry());
				System.out.println("省份：          " + originalUserInfo.getProvince());
				System.out.println("城市：          " + originalUserInfo.getCity());
				System.out.println("语言：          " + originalUserInfo.getLanguage());
				System.out.println("头像：          " + originalUserInfo.getHeadImgUrl());
	}

	public void CreatePermanentQRCode(String appid, String appsecret) {
		//获取接口访问凭据
		String accessToken=TokenExecute.getToken("wxf9b54a0426aab166", "8481d5df777deb142eb3a18b2691f95b").getAccessToken();
		//创建永久二维码
		String ticket=QRCodeExecute.createPermanentQRCode(accessToken, 617);
		System.out.println("ticket:"+ticket);
	}

	public void CreateTemporaryQRCode(String appid, String appsecret,
			int expireSeconds, int sceneId) {
		//获取接口访问凭据
				String accessToken=TokenExecute.getToken(appid, appsecret).getAccessToken();
				//创建临时二维码
				WeixinQRCodeDTO weixinQRCode=QRCodeExecute.createTemporaryQRCode(accessToken, expireSeconds, sceneId);
				//临时二维码的ticket
				System.out.println(weixinQRCode.getTicket());
				//临时二维码的有效时间
				System.out.println(weixinQRCode.getExpireSeconds());
		
	}

	public void GetQRCodeByTicket(String ticket, String savePath) {
		QRCodeExecute.getQRCode(ticket, savePath);
	}

	public void GetAttentionList(String appid, String appsecret) {
		String accessToken=TokenExecute.getToken(appid, appsecret).getAccessToken();
		WeixinUserListDTO weixinUserList = GetUserInfoExecute.getUserList(accessToken, "");
		System.out.println("总关注用户数：" + weixinUserList.getTotal());
		System.out.println("本次获取用户数：" + weixinUserList.getCount());
		System.out.println("OpenID列表：" + weixinUserList.getOpenIdList().toString());
		System.out.println("next_openid：" + weixinUserList.getNextOpenId());
	}
	
	//查询分组
	public void GetWeixinGroup(String appid, String appsecret) {
		String accessToken=TokenExecute.getToken(appid, appsecret).getAccessToken();
		List<WeixinGroupDTO> groupList = GroupExecute.getGroups(accessToken);
		// 循环输出各分组信息
		for (WeixinGroupDTO group : groupList) {
			System.out.println("ID:"+group.getId()+" 名称:"+group.getName()+" 用户数："+group.getCount());
		}
	}

	public void CreateWeixinGroup(String appid, String appsecret,
			String groupName) {
		String accessToken=TokenExecute.getToken(appid, appsecret).getAccessToken();
		WeixinGroupDTO group = GroupExecute.createGroup(accessToken, groupName);
		System.out.println(String.format("成功创建分组：%s id：%d", group.getName(), group.getId()));

	}

	@Override
	public void UpdateWeixinGroupName(String appid, String appsecret,
			int groupId, String groupName) {
		String accessToken=TokenExecute.getToken(appid, appsecret).getAccessToken();
		GroupExecute.updateGroup(accessToken, groupId, groupName);
	}

	public void ChangeUserWeixinGroup(String appid, String appsecret,
			String openId, int groupId) {
		String accessToken=TokenExecute.getToken(appid, appsecret).getAccessToken();
		GroupExecute.updateMemberGroup(accessToken, openId, groupId);
	}

}
